import os
import shutil
import time  
import logging
from watchdog.observers import Observer  
from watchdog.events import FileSystemEventHandler, LoggingEventHandler
from watchdog.events import DirModifiedEvent

  

class MyHandler(FileSystemEventHandler): 
    def __init__(self, srcPath='', basePath=''):
        self.srcPath =  ''
        self.targetPath = ''
        self.basePath = ''


    def on_modified(self, event):
        if event.is_directory:
            print("directory modified:{0}".format(event.src_path))
        else:
            print("file modified:{0}".format(event.src_path))
            self.copyFile(event)

    def copyFile(self, event):
        print('拷贝文件...', event.src_path)
        shutil.copy(event.src_path, 'copy' + event.src_path)
        # copyfile(src, target)


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')

    event_handler = MyHandler()  
    observer = Observer()  
    watch = observer.schedule(event_handler, path='.\\新建文件夹', recursive=True)
    print('start watchdog')
    event_handler2 = LoggingEventHandler()
    # observer.add_handler_for_watch(event_handler2, watch)
    observer.start()
    try:  
        while True:  
            time.sleep(1)  
    except KeyboardInterrupt:  
        observer.stop()  
    observer.join()